package SmartQuestions;

import questionsObjects.*;

/**
 * Created with IntelliJ IDEA.
 * User: xpk
 * Date: 11/19/13
 * Time: 2:47 PM
 * To change this template use File | Settings | File Templates.
 */
public class SmartQuestions {
    private final Questions questions;
    private ThemesOfQuestion themesOfQuestion;
    private ComplexityOfQuestion complexityOfQuestion;
    private UserState userState;
    private int currentIndexOfQuestion;
    private String currentTheme;
    private int currentComplexity;

    public SmartQuestions(Questions questions) {
        this.questions = questions;
        this.themesOfQuestion = new ThemesOfQuestion(this.questions);
        this.currentTheme = this.themesOfQuestion.getCurrentTheme();
        this.complexityOfQuestion = new ComplexityOfQuestion(currentTheme, questions);
        this.currentComplexity = this.complexityOfQuestion.growingUpComplexity();
        this.userState = new UserState();
    }

    public boolean hasQuestion() {
        if (!checkState()) {
            return false;
        }

        return true;
    }

    public String getQuestion() {
        System.out.println("currentTheme - " + currentTheme);
        System.out.println("currentComplexity - " + currentComplexity);

        return questions.getQuestion(currentIndexOfQuestion);
    }

    public String getAnswer() {
        return questions.getAnswers(currentIndexOfQuestion);
    }

    public boolean isCorrect(String answer) {
        boolean isCorrect;
        isCorrect = questions.isCorrectAnswer(currentIndexOfQuestion, answer);
        userState.pushAnswer(currentIndexOfQuestion, isCorrect);
        System.out.format("Your answer is %s %n", (isCorrect ? "true" : "false"));
        return isCorrect;
    }

    public QuestionHistory getQuestionHistory() {
        return userState.getQuestionHistory();
    }

    private boolean checkState() {
        if (userState.isGrowDownOrUpComplexity(false)) {
            currentComplexity = complexityOfQuestion.growingDownComplexity();
        }

        if (userState.isGrowDownOrUpComplexity(true)) {
            currentComplexity = complexityOfQuestion.growingUpComplexity();
        }

        currentIndexOfQuestion = questionFinder();

        if (currentComplexity == -1 || currentIndexOfQuestion == -1) {
            currentTheme = themesOfQuestion.getCurrentTheme();
            complexityOfQuestion = new ComplexityOfQuestion(currentTheme, questions);
            currentComplexity = complexityOfQuestion.growingUpComplexity();
            currentIndexOfQuestion = questionFinder();

            if (currentTheme == "" || currentComplexity == -1 || currentIndexOfQuestion == -1) {
                return false;
            }
        }
        return true;
    }

    private int questionFinder() {
        currentIndexOfQuestion = -1;
        for (int i = 0; i < questions.length(); ++i) {
            if (questions.getTheme(i).equals(currentTheme) && questions.getComplexity(i) == currentComplexity) {
                if (!userState.haveDuplicatesInHistory(i)) {
                    currentIndexOfQuestion = i;
                    break;
                }
            }
        }
        return currentIndexOfQuestion;
    }
}
